package cn.cvs.controller;

import cn.cvs.pojo.Apply;
import cn.cvs.pojo.Comment;
import cn.cvs.pojo.SysUser;
import cn.cvs.pojo.User;
import cn.cvs.service.comment.CommentService;
import cn.cvs.utils.Constants;
import cn.cvs.utils.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/comments")
public class CommentController extends BaseController{

    @Autowired
    private CommentService commentService;

    @PostMapping("/add")
    public String addComment(Comment comment, HttpSession session) {
        User user = (User) session.getAttribute(Constants.USER_SESSION);
        comment.setUserId(user.getId());
        commentService.addComment(comment);
        return "redirect:/sys/pet/toUpdate?id="+comment.getPetId(); // 这里假设宠物ID为1，实际应动态获取
    }

    @GetMapping("/list/{petId}")
    public String getCommentsByPetId(@PathVariable Integer petId, Model model) {
        List<Comment> comments = commentService.getCommentsByPetId(petId);
        model.addAttribute("comments", comments);
        return "commentList"; // 返回的视图名，对应上面的HTML文件名
    }

    @DeleteMapping("/delete/{commentId}")
    @ResponseBody
    public String deleteComment(@PathVariable Integer commentId) {
        commentService.deleteComment(commentId);
        return "评论删除成功";
    }

    @RequestMapping(value="/toList")
    public String getUserList(Model model
            ,@RequestParam(value="state",required=false) Integer state
            ,@RequestParam(value="pageIndex",required=false, defaultValue = "1") Integer pageIndex){

        //设置页面容量
        int pageSize = Constants.pageSize;
        try{
            //总数量（表）
            int totalCount	= commentService.count(state);
            //总页数
            PageSupport pages=new PageSupport();
            pages.setCurrentPageNo(pageIndex);
            pages.setPageSize(pageSize);
            pages.setTotalCount(totalCount);
            int totalPageCount = pages.getTotalPageCount();
            //控制首页和尾页
            if(pageIndex > totalPageCount){
                pageIndex = totalPageCount;
            }
            List<Comment> comment = commentService.list(state, pageIndex, pageSize);
            model.addAttribute("comment", comment);
            model.addAttribute("totalPageCount", totalPageCount);
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("currentPageNo", pageIndex);
        }catch(Exception e){
            e.printStackTrace();
            return "redirect:/sys/toSysError";
        }
        return "comments/list";
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable Integer id, Model model) {
        Comment byId = commentService.getById(id);
        model.addAttribute("comments", byId);
        return "comments/update"; // 返回的视图名，对应上面的HTML文件名
    }

    @PostMapping("/update")
    public String view(Comment comment) {
        int count = commentService.update(comment);
        if (count > 0 ) {
            return "redirect:/comments/toList";
        }
        return "comments/update"; // 返回的视图名，对应上面的HTML文件名
    }
}